package gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Frame;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JFileChooser;
import javax.swing.JTabbedPane;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import pack.MyReader;
import structures.MainFile;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;

public class MyFrame extends JFrame {

	private JPanel contentPane;
	private JTabbedPane tabbed;
	private MainFile mf;
	private MyReader reader;

	/**
	 * Create the frame.
	 */
	public MyFrame(List<JPanel> tabs, List<String> names) {
		_init();
		for(int i = 0 ; i < tabs.size() ; i++){
			JPanel panel = tabs.get(i);
			String name = names.get(i);
			tabbed.addTab(name, panel);
		}
	}
	
	public void clearTabs()
	{
		tabbed.removeAll();
	}
	
	public void addTab(Component tab, String name)
	{
		tabbed.addTab(name, tab);
	}
	
	public MyFrame()
	{		
		_init();
	}
	
	protected void fillMe(File file)
	{
		clearTabs();
		MainFile mf = new MainFile(new MyReader(file), false);
		FrameBuilder.fill(this, mf);
	}
	
	private void _init()
	{
		setTitle("TahoLogic");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setExtendedState(Frame.MAXIMIZED_BOTH);  
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);
		
		JMenuItem mntmOpenFile = new JMenuItem("Open file");
		mntmOpenFile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				final JFileChooser fc = new JFileChooser();
				int returnVal = fc.showOpenDialog(getComponent(0));
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					fillMe(fc.getSelectedFile());
				}
			}
		});
		mnFile.add(mntmOpenFile);
		
		JMenuItem mntmExit = new JMenuItem("Exit");
		mntmExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false); //you can't see me!
				dispose(); //Destroy the JFrame object
			}
		});
		mnFile.add(mntmExit);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		tabbed = new JTabbedPane(JTabbedPane.TOP);
		contentPane.add(tabbed, BorderLayout.CENTER);
	}
}
